/**
 * 
 */
package com.res.common.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.res.simulator.handler.SubmitOrderHandler;
import com.res.simulator.handler.UserManagementHandler;
import com.res.simulator.model.StoreFrontEligibilityModel;
import com.res.simulator.model.UserManagementModel;
import com.res.simulator.view.SubmitOrderView;
import com.res.simulator.view.UserManagementView;

/**
 * @author Dhanushka Ariyasingha.
 * 
 */
@Controller
@SessionAttributes
public class UserManagementController extends BaseController {

	@RequestMapping("/usermanagementhome")
	public String home(
			@ModelAttribute("UserManagementModel") UserManagementModel usermanagementModel) {

		return "usermanagementlayout";
	}

	@RequestMapping(value = "/handleuser", method = RequestMethod.GET, params = "Create")
	protected String createNewUser(
			@ModelAttribute("UserManagementModel") UserManagementModel usermanagementModel,
			final BindingResult result) throws Exception {

		final UserManagementHandler handler = new UserManagementHandler();
		usermanagementModel.setCommand("CREATE");
		final UserManagementView view = handler.process(usermanagementModel);
		
		List<UserManagementView> viewCollection = (List<UserManagementView>) getSession().getAttribute("UserManagementViewCollection");
		
		if (null == viewCollection) {
			viewCollection = new ArrayList<UserManagementView>();
		}

		viewCollection.add(view);		
		getSession().setAttribute("UserManagementViewCollection", viewCollection);
		
		return "redirect:usermanagementhome.htm";
	}
	
	@RequestMapping(value = "/handleuser", method = RequestMethod.GET, params = "Update")
	protected String updateUser(
			@ModelAttribute("UserManagementModel") UserManagementModel usermanagementModel,
			final BindingResult result) throws Exception {

		final UserManagementHandler handler = new UserManagementHandler();
		usermanagementModel.setCommand("UPDATE");
		final UserManagementView view = handler.process(usermanagementModel);
		
		List<UserManagementView> viewCollection = (List<UserManagementView>) getSession().getAttribute("UserManagementViewCollection");
		
		if (null == viewCollection) {
			viewCollection = new ArrayList<UserManagementView>();
		}

		viewCollection.add(view);
		getSession().setAttribute("UserManagementViewCollection", viewCollection);
		
		return "redirect:usermanagementhome.htm";
	}
	
	@RequestMapping(value = "/handleuser", method = RequestMethod.GET, params = "Clear")
	protected String clearUserHistory(
			@ModelAttribute("UserManagementModel") UserManagementModel usermanagementModel,
			final BindingResult result) throws Exception {

		final List<UserManagementView> viewCollection = (List<UserManagementView>) getSession().getAttribute("UserManagementViewCollection");
		
		if (null != viewCollection) {
			viewCollection.clear();
		}
		
		getSession().setAttribute("UserManagementViewCollection", viewCollection);
		
		return "redirect:usermanagementhome.htm";
	}

}
